/**
 * 性别
 * @param {*} sex 
 * @returns  性别：1男 2女
 */
export function transferSex(sex) {
    var sexName = ''
    switch (sex) {
        case '1':
            sexName = '男'
            break
        case '2':
            sexName = '女'
            break
        default:
            sexName = '-'
            break
    }
    return sexName
}


/**
 * PC管理员状态
 * @param {*} state 
 * @returns  用户状态：1在用; 2挂起;
 */
export function transferPcState(state) {
    var stateName = ''
    switch (state) {
        case '1':
            stateName = '在用'
            break
        case '2':
            stateName = '挂起'
            break
        default:
            stateName = '-'
            break
    }
    return stateName
}


/**
 * 格式化时间
 * 年月日 时分秒
 * @param value
 * @param row
 * @param index
 * @returns {string}
 */
export function dateFormatter(value) {
    if (value != null) {
        var date = new Date(value)
        var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        var currentDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        var hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
        var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
        var ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        return date.getFullYear() + '-' + month + '-' + currentDate + ' ' + hh + ':' + mm + ':' + ss
    }
    return '-'
}

export function msToTime(duration) {
    if (duration != null) {
        let milliseconds = parseInt((duration % 1000) / 100)
        , seconds = parseInt((duration / 1000) % 60)
        , minutes = parseInt((duration / (1000 * 60)) % 60)
        , hours = parseInt((duration / (1000 * 60 * 60)) % 24);
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;
        return hours + ":" + minutes + ":" + seconds;
    }
    return '-'
}


/**
 * 格式化时间
 * 年月日
 * @returns {string}
 */
export function dateFormatterDay(value) {
    if (value != null) {
        var date = new Date(value)
        var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        var currentDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        return date.getFullYear() + '-' + month + '-' + currentDate
    }
    return '-'
}

/**
 * 格式化时间
 * 年月
 * @returns {string}
 */
export function dateFormatterMonth(value) {
    if (value != null) {
        var date = new Date(value)
        var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        return date.getFullYear() + '-' + month
    }
    return '-'
}

/**
 * 计算两个日期相差天数
 * @param {*} date1 
 * @param {*} date2 
 * @returns 
 */
export function getDaysBetweenDates(date1, date2) {
    // 将日期字符串转换为Date对象
    var d1 = new Date(date1);
    var d2 = new Date(date2);
    // 计算两个日期的时间差（毫秒）
    var timeDiff = Math.abs(d2.getTime() - d1.getTime());
    // 计算天数
    var days = Math.ceil(timeDiff / (1000 * 3600 * 24));
    return days;
}

// 给日期添加指定月份并返回格式化后的字符串
export function addMonthsAndFormat(inputDateStr, monthsToAdd) {
    // 创建日期对象
    const startDate = new Date(inputDateStr);
    const year = startDate.getFullYear();
    const month = startDate.getMonth();
    const day = startDate.getDate();
    const hours = startDate.getHours();
    const minutes = startDate.getMinutes();
    const seconds = startDate.getSeconds();
    // 添加月份
    const newDate = new Date(year, month + monthsToAdd, day, hours, minutes, seconds);
    // 格式化日期字符串
    const yearStr = newDate.getFullYear();
    const monthStr = String(newDate.getMonth() + 1).padStart(2, '0');
    const dayStr = String(newDate.getDate()).padStart(2, '0');
    const hoursStr = String(newDate.getHours()).padStart(2, '0');
    const minutesStr = String(newDate.getMinutes()).padStart(2, '0');
    const secondsStr = String(newDate.getSeconds()).padStart(2, '0');

    return `${yearStr}-${monthStr}-${dayStr} ${hoursStr}:${minutesStr}:${secondsStr}`;

}

/**
 * 获取X天前的日期字符串
 * @returns 
 */
export function getXDaysAgo(X) {
    const today = new Date();
    const sevenDaysAgo = new Date(today);
    sevenDaysAgo.setDate(sevenDaysAgo.getDate() - X);
    const year = sevenDaysAgo.getFullYear();
    const month = ('0' + (sevenDaysAgo.getMonth() + 1)).slice(-2); // 月份是从0开始的
    const day = ('0' + sevenDaysAgo.getDate()).slice(-2);
    return `${year}-${month}-${day}`;
}

/**
 * JSON数组去重
 * @params {Array} arr 传入的JSON数组
 * @param  {String} attrName 根据哪个属性名去重
 * @return {Array} 返回新的去重后的数组
 * */
export function delRepeatJson(arr = [], attrName = '') {
    var temp = {}; //用于name判断重复
    var result = []; //最后的新数组
    arr.forEach(function (item, index) {
        if (!temp[item[attrName]]) {
            result.push(item);
            temp[item[attrName]] = true;
        }
    });
    return result;
}

/**
 * 微信用户是否绑定
 * @param {*} bind 
 * @returns  0未绑定 1已绑定
 */
export function transferWxUserBind(bind) {
    var bindName = ''
    switch (bind) {
        case '0':
            bindName = '未绑定'
            break
        case '1':
            bindName = '已绑定'
            break
        default:
            bindName = '-'
            break
    }
    return bindName
}


/**
 * 微信用户账号状态
 * @param {*} wxUserState 
 * @returns  0挂起 1在用
 */
export function transferWxUserState(wxUserState) {
    var wxUserStateName = ''
    switch (wxUserState) {
        case '1':
            wxUserStateName = '在用'
            break
        case '2':
            wxUserStateName = '待审核'
            break
        default:
            wxUserStateName = '-'
            break
    }
    return wxUserStateName
}


/**
 * 微信用户类型
 * @param {*} wxUserType 
 * @returns  0工程师 1客户
 */
export function transferWxUserType(wxUserType) {
    var wxUserTypeName = ''
    switch (wxUserType) {
        case '0':
            wxUserTypeName = '工程师'
            break
        case '1':
            wxUserTypeName = '客户'
            break
        default:
            wxUserTypeName = '-'
            break
    }
    return wxUserTypeName
}

/**
 * 微信用户类型
 * @param {*} wxUserType 
 * @returns  0工程师 1客户
 */
export function transferWxUser_dailyState(dailyState) {
    var result = ''
    switch (dailyState) {
        case '0':
            result = '闲'
            break
        case '1':
            result = '较忙'
            break
        case '2':
            result = '忙'
            break
        default:
            result = '-'
            break
    }
    return result
}